package com.sso.managedBean;

import javax.faces.context.FacesContext;

import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.user.util.Browser;

import com.base.common.provider.CommonKeySelectItemProvider;
import com.base.commons.SpringBeanHolder;
import com.base.utils.IdUtils;
import com.sso.entity.DicHuman;
import com.sso.entity.DicSystem;
import com.sso.service.IDicHumanService;
import com.sso.service.IDicSystemService;
@ManagedBean(name = "sso_addHumanBean", scope = ManagedBeanScope.REQUEST)
public class AddHumanBean {
	protected IDicHumanService dicHumanService = SpringBeanHolder
	.getBean("dicHumanService");
	protected IDicSystemService dicSystemService = SpringBeanHolder
	.getBean("dicSystemService");
	@Bind
	@Accessible
	@SaveState
	String id;
	
	@Accessible
	@SaveState
	private String systemRequestId = FacesContext.getCurrentInstance()
			.getExternalContext().getRequestParameterMap().get("systemId");
	
	@Bind
	@Accessible
	@SaveState
	protected String humanID;
	
	protected DicSystem system;
	protected DicHuman human;
	// 系统名称
	@Bind(id = "humanName", attribute = "value")
	protected String humanName;
	// 密码
	@Bind(id = "humanPassword", attribute = "value")
	protected String humanPassword;
	//系统名称
	@Bind(id = "systemName", attribute = "value")
	protected String systemName;
	// 系统
	//@Bind(id = "systemId", attribute = "value")
	//protected String systemId;
	
	@Inject
	private FacesContext context;

	@SaveState
	private String first;
	
	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			id = context.getExternalContext().getRequestParameterMap()
					.get("humanId");
			if (id != null && !"".equals(id)) {
				human = dicHumanService.getByHumanId(id);
				id=human.getId();
				humanName=human.getHumanName();
				humanPassword=human.getHumanPassword();
				systemName = human.getSystem().getSystemName();
			} else {
				id = IdUtils.getUUID();
				DicSystem ds = dicSystemService.getBySystemID(systemRequestId);
				systemName = ds.getSystemName();
			}
		}
	}
	@Action(id = "save")
	public void save() {
		if(id!=null &&!"".equals(id)){
			human = dicHumanService.getByHumanId(id);
			if(human!=null){
				human.setHumanName(humanName);
				human.setHumanPassword(humanPassword);
			}else{
				human = new DicHuman();
				human.setId(id);
				human.setHumanName(humanName);
				human.setHumanPassword(humanPassword);
			}
			DicSystem ds = dicSystemService.getBySystemID(systemRequestId);
			
			if(ds!=null){
				human.setSystem(ds);
				dicHumanService.saveDicHuman(human);
			}
		
		}
		String script = "closeWindow('humanList_relation')";
		Browser.execClientScript("window.parent", script);
	}
}
